<?

require_once '../lib.php';
error_reporting(E_ERROR);

$fp = new FP_Custom();

if (isset($_GET['clear'])) {
	if ($dir = opendir(CACHE_DIR)) {
		while ($file = readdir($dir)) {
			unlink(CACHE_DIR.$file);
		}
	}
	echo '<fb:redirect url="'.$fp->canvas_url.'/admin/cache/" />';
}

$total_size = 0;
if ($dir = opendir(CACHE_DIR)) {
	while ($file = readdir($dir)) {
		if (filetype($file) != 'dir') {
			$files[] = $file;
			$total_size = $total_size + filesize(CACHE_DIR.$file);
		}
	}
}

$number_of_files = count($files);

function getPrettySize($bytes) {
    $symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
    $exp = floor(log($bytes) / log(1024));

    return sprintf('%.2f '.$symbols[$exp], ($bytes / pow(1024, floor($exp))));
}

?>

<style>
td { padding: 3px; }
</style>

<fb:title>Cache Management</fb:title>
<link rel="stylesheet" type="text/css" media="screen" href="<?=$fp->callback_url?>/styles.php?v=<?=trim(REVISION)?>&amp;p=admin" />

<fb:if-is-user uid="<?=$fp->get_app_developers()?>">
	
	<div id="fp_header">
		<a href="<?=$fp->canvas_url?>/" title="Home"><img src="http://www.flickr.com/images/buddyicon.jpg" /></a>
		<h2>
			<a href="<?=$fp->canvas_url?>/" title="Home"><img src="<?=$fp->app_properties['icon_url']?>" /></a>
			<fb:application-name linked="false" /> Cache Management
		</h2>
		<div class="headertabs"><div class="tabwrapper"><div class="thetabs">
			<a href="<?=$fp->canvas_url?>/admin/lookup/">
				User Lookup
			</a>
			<a class="selected" href="<?=$fp->canvas_url?>/admin/cache/">
				Cache Management
			</a>
		</div></div><div class="clearfix"></div></div>
	</div>
	
	<div class="boxes clearfix">
		<div class="left_column">
			
			<div class="box" id="account_box">
				<h3>phpFlickr</h3>
				<h4 class="clearfix">
					<span class="left">
						<?=getPrettySize($total_size)?> in <?=$number_of_files?> files
					</span>
					<span class="right">
						<a href="<?=$fp->canvas_url?>/admin/cache/?clear">Clear</a>
					</span>
				</h4>
				<div class="inside">
					
					<p><a href="" clicktotoggle="filetable">Show details</a></p>
					
					<table id="filetable" style="display:none;">
						<tr>
							<th>Filename</th>
							<th>Filesize</th>
						</tr>
						<? $i = 0; foreach ($files as $file): ?>
						<tr<?=($i % 2 == 0) ? ' style="background-color: #ECEEF4;"' : '' ?>>
							<td><?=$file?></td>
							<td><?=filesize(CACHE_DIR.$file)?></td>
						</tr>
						<? $i++; endforeach; ?>
					</table>
					
				</div>
			</div>
			
		</div>
	</div>
	
<fb:else>
	<fb:error message="You're not allowed in here!" />
</fb:else>
</fb:if-is-user>